/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;

/**
 * @author Administrator
 */
public class MailClientMobi extends MIDlet implements CommandListener{
   public Display display;
   public Screen screen;
   private Command backCommand = new Command("Back", Command.BACK, 1);
   private Command okCommand = new Command("Send", Command.OK, 1);
   private TextField toField,subjField,contentField;

    private String server = "localhost";

    private int port = 9999;
    DataInputStream dataInputStream;
    DataOutputStream  dataOutputStream;
    DataHandler dataHandler;
   // public CustomInputCanvas screen;
    private SocketConnection socketConnection;
    public void startApp() {
        try
        {

        display=Display.getDisplay(this);

             String url = "socket://" + server + ":" + port;
            socketConnection = (SocketConnection) Connector.open(url);
            dataInputStream = new DataInputStream(socketConnection.openInputStream());
            dataOutputStream = new DataOutputStream(socketConnection.openOutputStream());
            dataHandler = new DataHandler(dataOutputStream, dataInputStream);

      //  screen=new CustomInputCanvas();
       screen=new Screen(this,dataHandler);
        display.setCurrent(screen);
        }
        catch(Exception ex)
        {
            System.out.println("error start midlet"+ex.toString());
        }


    }
    public void showForm()
    {
        Form mainForm=new Form("Compose ");
        toField=new TextField("To", "", 100, TextField.EMAILADDR);
        subjField=new TextField("Subj", "", 100, TextField.ANY);
        contentField=new TextField("Content", "", 100, TextField.ANY);
         mainForm.append(toField);
         mainForm.append(subjField);
         mainForm.append(contentField);
         mainForm.addCommand(okCommand);
         mainForm.addCommand(backCommand);
         mainForm.setCommandListener(this);



        display.setCurrent(mainForm);

    }


    public void pauseApp() {
        Runtime.getRuntime().gc();
    }

    public void destroyApp(boolean unconditional) {

           Runtime.getRuntime().gc();
            if(display!=null)
                display = null;
            this.notifyDestroyed();
     }
    
      public void commandAction(Command c, Displayable s) {
        if (c == backCommand) {
            destroyApp(false);
            notifyDestroyed();
        }

        if(c==okCommand)
        {
            try
            {
            dataHandler.sendData(DataHandler.SEND_ADDRESS,toField.getString());
            dataHandler.sendData(DataHandler.SEND_SUBJ,subjField.getString());
            dataHandler.sendData(DataHandler.SEND_CONTENT,contentField.getString());
            dataHandler.sendData(DataHandler.SEND_ACTION, null);
            System.out.println("dang send");
            }
            catch(Exception ex)
            {
                System.out.println("error"+ex.toString());
            }

        }
    }
}
